package com.firebird.app.kotlin.stock.trend.view

import android.annotation.SuppressLint
import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import android.widget.TextView
import com.firebird.app.kotlin.stock.trend.R


/**
 * 自定义统一风格的通用对话框
 * @author ljk
 * @since  2019年4月13日
 */
class CustomDialog : Dialog {
    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, theme: Int) : super(context, theme) {}

    class Builder(private val context: Context) {
        private var contentView: View? = null
        private var title: String? = null
        private var checkedItem = 0
        private var isSingleChoice = false
        //private CharSequence[] items;
        private var positiveButtonText: String? = null
        private var negativeButtonText: String? = null
        private var singleChoiceClickListener: DialogInterface.OnClickListener? = null
        private var positiveButtonClickListener: DialogInterface.OnClickListener? = null
        private var negativeButtonClickListener: DialogInterface.OnClickListener? = null
        fun setContentView(v: View?): Builder {
            contentView = v
            return this
        }

        fun setTitle(title: Int): Builder {
            this.title = context.getText(title) as String
            return this
        }

        fun setTitle(title: String?): Builder {
            this.title = title
            return this
        }

        fun setPositiveButton(positiveButtonText: Int, listener: DialogInterface.OnClickListener?): Builder {
            this.positiveButtonText = context.getText(positiveButtonText) as String
            positiveButtonClickListener = listener
            return this
        }

        fun setPositiveButton(positiveButtonText: String?, listener: DialogInterface.OnClickListener?): Builder {
            this.positiveButtonText = positiveButtonText
            positiveButtonClickListener = listener
            return this
        }

        fun setNegativeButton(negativeButtonText: Int, listener: DialogInterface.OnClickListener?): Builder {
            this.negativeButtonText = context.getText(negativeButtonText) as String
            negativeButtonClickListener = listener
            return this
        }

        fun setNegativeButton(negativeButtonText: String?, listener: DialogInterface.OnClickListener?): Builder {
            this.negativeButtonText = negativeButtonText
            negativeButtonClickListener = listener
            return this
        }

        fun setSingleChoiceItems(itemsId: Int, checkedItem: Int, listener: DialogInterface.OnClickListener?): Builder { //items = context.getResources().getTextArray(itemsId);
            singleChoiceClickListener = listener
            this.checkedItem = checkedItem
            isSingleChoice = true
            return this
        }

        @SuppressLint("InflateParams")
        fun create(): CustomDialog {
            val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            val dialog = CustomDialog(context, R.style.FontDialog)
            val layout: View = inflater.inflate(R.layout.layout_dialog_custom, null)
            dialog.setContentView(layout, ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT))
            val titleTv = layout.findViewById<View>(R.id.title) as TextView
            if (title != null && "" == title) {
                titleTv.text = title //设置标题
            } else {
                titleTv.visibility = View.GONE
            }
            if (positiveButtonText != null) {
                val ptv = layout.findViewById<View>(R.id.cancle) as TextView
                ptv.text = positiveButtonText
                if (positiveButtonClickListener != null) {
                    ptv.setOnClickListener { positiveButtonClickListener!!.onClick(dialog, DialogInterface.BUTTON_POSITIVE) }
                }
            } else {
                layout.findViewById<View>(R.id.cancle).visibility = View.GONE
            }
            if (negativeButtonText != null) {
                val ntv = layout.findViewById<View>(R.id.sure) as TextView
                if (negativeButtonClickListener != null) {
                    ntv.setOnClickListener { negativeButtonClickListener!!.onClick(dialog, DialogInterface.BUTTON_NEGATIVE) }
                }
            } else {
                layout.findViewById<View>(R.id.sure).visibility = View.GONE
            }
            if (isSingleChoice) {
                val butBig = layout.findViewById<View>(R.id.font_big) as ImageButton
                val butNormal = layout.findViewById<View>(R.id.font_normal) as ImageButton
                val butSmall = layout.findViewById<View>(R.id.font_small) as ImageButton
                butBig.setOnClickListener {
                    checkedItem = 0
                    when (checkedItem) {
                        0 -> {
                            butBig.setImageResource(R.mipmap.checkbox_checked)
                            butNormal.setImageResource(R.mipmap.checkbox_nomal)
                            butSmall.setImageResource(R.mipmap.checkbox_nomal)
                        }
                        1 -> {
                            butBig.setImageResource(R.mipmap.checkbox_nomal)
                            butNormal.setImageResource(R.mipmap.checkbox_checked)
                            butSmall.setImageResource(R.mipmap.checkbox_nomal)
                        }
                        2 -> {
                            butBig.setImageResource(R.mipmap.checkbox_nomal)
                            butNormal.setImageResource(R.mipmap.checkbox_nomal)
                            butSmall.setImageResource(R.mipmap.checkbox_checked)
                        }
                        else -> {
                            butBig.setImageResource(R.mipmap.checkbox_checked)
                            butNormal.setImageResource(R.mipmap.checkbox_nomal)
                            butSmall.setImageResource(R.mipmap.checkbox_nomal)
                        }
                    }
                    singleChoiceClickListener!!.onClick(dialog, checkedItem)
                }
                butNormal.setOnClickListener {
                    checkedItem = 1
                    when (checkedItem) {
                        0 -> {
                            butBig.setImageResource(R.mipmap.checkbox_checked)
                            butNormal.setImageResource(R.mipmap.checkbox_nomal)
                            butSmall.setImageResource(R.mipmap.checkbox_nomal)
                        }
                        1 -> {
                            butBig.setImageResource(R.mipmap.checkbox_nomal)
                            butNormal.setImageResource(R.mipmap.checkbox_checked)
                            butSmall.setImageResource(R.mipmap.checkbox_nomal)
                        }
                        2 -> {
                            butBig.setImageResource(R.mipmap.checkbox_nomal)
                            butNormal.setImageResource(R.mipmap.checkbox_nomal)
                            butSmall.setImageResource(R.mipmap.checkbox_checked)
                        }
                        else -> {
                            butBig.setImageResource(R.mipmap.checkbox_checked)
                            butNormal.setImageResource(R.mipmap.checkbox_nomal)
                            butSmall.setImageResource(R.mipmap.checkbox_nomal)
                        }
                    }
                    singleChoiceClickListener!!.onClick(dialog, checkedItem)
                }
                butSmall.setOnClickListener {
                    checkedItem = 2
                    when (checkedItem) {
                        0 -> {
                            butBig.setImageResource(R.mipmap.checkbox_checked)
                            butNormal.setImageResource(R.mipmap.checkbox_nomal)
                            butSmall.setImageResource(R.mipmap.checkbox_nomal)
                        }
                        1 -> {
                            butBig.setImageResource(R.mipmap.checkbox_nomal)
                            butNormal.setImageResource(R.mipmap.checkbox_checked)
                            butSmall.setImageResource(R.mipmap.checkbox_nomal)
                        }
                        2 -> {
                            butBig.setImageResource(R.mipmap.checkbox_nomal)
                            butNormal.setImageResource(R.mipmap.checkbox_nomal)
                            butSmall.setImageResource(R.mipmap.checkbox_checked)
                        }
                        else -> {
                            butBig.setImageResource(R.mipmap.checkbox_checked)
                            butNormal.setImageResource(R.mipmap.checkbox_nomal)
                            butSmall.setImageResource(R.mipmap.checkbox_nomal)
                        }
                    }
                    singleChoiceClickListener!!.onClick(dialog, checkedItem)
                }
                when (checkedItem) {
                    0 -> {
                        butBig.setImageResource(R.mipmap.checkbox_checked)
                        butNormal.setImageResource(R.mipmap.checkbox_nomal)
                        butSmall.setImageResource(R.mipmap.checkbox_nomal)
                    }
                    1 -> {
                        butBig.setImageResource(R.mipmap.checkbox_nomal)
                        butNormal.setImageResource(R.mipmap.checkbox_checked)
                        butSmall.setImageResource(R.mipmap.checkbox_nomal)
                    }
                    2 -> {
                        butBig.setImageResource(R.mipmap.checkbox_nomal)
                        butNormal.setImageResource(R.mipmap.checkbox_nomal)
                        butSmall.setImageResource(R.mipmap.checkbox_checked)
                    }
                    else -> {
                        butBig.setImageResource(R.mipmap.checkbox_checked)
                        butNormal.setImageResource(R.mipmap.checkbox_nomal)
                        butSmall.setImageResource(R.mipmap.checkbox_nomal)
                    }
                }
            }
            dialog.setContentView(layout)
            return dialog
        }

    }
}

